package com.example.administrator.wallet.view.activity.finds.kkbank;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.administrator.wallet.R;
import com.example.administrator.wallet.adapter.AssetListAdapter;
import com.example.administrator.wallet.constant.Api;
import com.example.administrator.wallet.intf.IRequestCallback;
import com.example.administrator.wallet.intf.IRequestManager;
import com.example.administrator.wallet.model.bean.MainBean;
import com.example.administrator.wallet.utils.RequestFactory;
import com.example.administrator.wallet.utils.SharedPrefsUtil;
import com.example.administrator.wallet.view.activity.asset.RecordActivity;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class CurrentDelegateFragment extends Fragment{
    private ListView lv_CurrentDelegateFragment;

    IRequestManager reqequestManager = RequestFactory.getReqequestManager();

    private List<MainBean.DataBean.ListBean> dataBeanList;
    public AssetListAdapter adapter;//CurrentDelegateAdapter
    private MainBean mainBean;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_current_delegate,container,false);

        lv_CurrentDelegateFragment = (ListView) view.findViewById(R.id.lv_CurrentDelegateFragment);

//        setStatuColor();
        initData();


        return view;
//        item    item_transaction_record   ExpenditureRecordActivity
    }
    public void initAdapter() {
        adapter = new AssetListAdapter(getActivity(), dataBeanList);
        lv_CurrentDelegateFragment.setAdapter(adapter);
    }
    private void initData() {
//        iDatas = new ArrayList<>();
        dataBeanList = new ArrayList<>();
        reqequestManager.httpPost(Api.BaseUrl + Api.Main_List, Api.getMainList(SharedPrefsUtil.getValue(getActivity(), "wallet", "token", null), ""), new IRequestCallback() {
            @Override
            public void onSuccess(String Response) {
                Gson gson = new Gson();
                mainBean = gson.fromJson(Response, MainBean.class);
                if (mainBean.getCode() == 200) {
                    if (mainBean.getData().getList() != null) {
                        dataBeanList.addAll(mainBean.getData().getList());
                        initAdapter();
                        OnClick();
//                        smart.finishRefresh();
                    } else {
                        initData();
                    }
                }
            }

            @Override
            public void onFailure(String error) {
//                smart.finishRefresh();
//                toast(error);
            }
        });
    }
    private void OnClick() {
        adapter.getClickPst(new AssetListAdapter.OnClick() {
            @Override
            public void Click(MainBean.DataBean.ListBean dataBean) {
                String Currency;//币种
                int id;//订单ID
                String market;//交易区(BTCUSDT/ETHUSDT/ETHBTC)
                //撤销
                reqequestManager.httpPost(Api.BaseUrl + Api.Cancel_Order, Api.getCancelOrder(SharedPrefsUtil.getValue(getActivity(), "wallet", "token", null), "",""), new IRequestCallback() {
                    @Override
                    public void onSuccess(String Response) {
                        Gson gson = new Gson();
                        mainBean = gson.fromJson(Response, MainBean.class);
                        if (mainBean.getCode() == 200) {
                            if (mainBean.getData().getList() != null) {
                                dataBeanList.addAll(mainBean.getData().getList());
                            } else {
                                initData();
                            }
                        }
                    }

                    @Override
                    public void onFailure(String error) {
                    }
                });
                Intent intent = new Intent(getActivity(), RecordActivity.class);
                intent.putExtra("datas", dataBean);
                getActivity().startActivity(intent);
            }
        });

//        smart.setOnRefreshListener(new OnRefreshListener() {
//            @Override
//            public void onRefresh(RefreshLayout refreshlayout) {
//                if (refreshlayout.isRefreshing()) {
//                    initData();
//                }
//            }
//        });
//
//        smart.setOnLoadmoreListener(null);
//        smart.setLoadmoreFinished(true);
    }
    @Override
    public void onResume() {
        super.onResume();
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }
}
